// pages/mine/updateInfo/update-info.js
import request from "../../../utils/request.js";
import local from "../../../utils/local";
import toast from "../../../utils/toast.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appid: 'wx15a7538fa77ddfc0',
    serviceUrl: '',
    avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    avatarUrl3: "",
    memberName:"",
    openId: '',
    jndiName: '',
    phoneCode: "",
    encryptedData: "",
    iv: "",
  },


  onChooseAvatar(e) {
    console.log(e)
    const avatarUrl = e.detail.avatarUrl
    console.log(avatarUrl)
    this.uploadHeaderImage(avatarUrl)
    // const that = this
    // wx.chooseMedia({
    //   sourceType: ['album', 'camera'],
    //   count: 1,
    //   mediaType: ['image','video'],
    //   maxDuration: 30,
    //   camera: 'back',
    //   success: function (res) {
    //     console.log(res)
    //     const tempDic = res["tempFiles"][0]
    //     const tempFilePath = tempDic["tempFilePath"]
    //     that.uploadHeaderImage(tempFilePath)
    //   },
    //   fail: function (err) {
    //    },
    // })
  },

  uploadHeaderImage(filePath) { 
    const that = this
    request.uploadFile(
      "/wx/uploadpicserv", 
      filePath, 
      this.data.openId,
      this.data.jndiName,
      {
        file: filePath,
        name: "pic"
      }
      ).then(res => {
        console.log(res)
        var data = JSON.parse(res.data)
        const memPicUrl = data.memPicUrl
        console.log(memPicUrl)
        let tempUrl = this.data.serviceUrl + memPicUrl
        console.log(tempUrl)
        that.setData({
          avatarUrl: tempUrl
        })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let url = request.SERVICE_URL
    const id = local.getLocal("openId")
    const tempJndiName = local.getLocal("jndiName")
    this.setData({
      serviceUrl: url,
      openId: id,
      jndiName: tempJndiName
    })
  },

  onNickNameInput(e) {
    let value = e.detail.value || "";
    this.setData({
      memberName: value
    });
  },

  onGetPhoneNumber(e) {
    if (this.data.memberName == '') {
      toast.showToast('会员姓名不能为空')
    }else {
      if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        toast.showToast('您已拒绝获取用户信息,将离开登录页')
        setTimeout(() => {
          nav.navigateBack("/index/index");
        }, 1500)
        return
      }
      let tempPhoneCode = e.detail.code || "";
      let tempEncryptedData = e.detail.encryptedData || "";
      let tempIv = e.detail.iv || "";
      this.setData({
        phoneCode: tempPhoneCode,
        encryptedData: tempEncryptedData,
        iv: tempIv,
      })
      this.getLoginCode()
    }
  },

  getLoginCode() {
    var that = this
    wx.login({
      success: (res) => {
        console.log(res)
        const code = res.code
        that.setData({
          js_code: code
        })
        that.getOpenId(code)
      },
    })
  },

  getOpenId(code) {
    const that = this
    let dic = {
      'appId': this.data.appid,
      'js_code': code,
      'jndiName': this.data.jndiName,
      'grant_type': 'authorization_code',
      'eventCode': '111',
    }
    console.log(dic)
    let url = '/paipanmemberservice?appId='+ this.data.appid +'&js_code=' + code +'&jndiName=' + this.data.jndiName +'&grant_type=authorization_code&eventCode=111'
    request.get(url, {}).then((res) => {
      console.log(res)
      const returnCode = res.return
      if (returnCode == 1) {
        const tempOpenId = res.openid
        const tempSessionKey = res.session_key
        that.setData({
          openId: tempOpenId,
          session_key: tempSessionKey
        })
        if (tempOpenId.length != 0) {
          that.getWxloginservlet()
        }else{
          toast.showToast('获得信息异常')
        }
        
      }
    })
    
  },

  getWxloginservlet() {
    const that = this;
    let dic = {
      appid: this.data.appid,
      secret: this.data.i_my,
      encrypdata: this.data.encryptedData,
      ivdata: this.data.iv,
      sessionkey: this.data.session_key,
      jndiName: this.data.jndiName,
      eventCode: '2',
      nickName: this.data.memberName,
      gender: '0',
      avatarUrl: this.data.avatarUrl,
      country: '',
      province: '',
      city: '',
      openId: this.data.openId,
      parentOpenId: '0'
    };
    console.log(dic)
    request.get("/wxloginservlet", dic).then(res => {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        const errmsg = res.errmsg
        toast.showToast(errmsg)
      }
        
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})